﻿
Imports System.IO


Public Class frmAffichProtocol


    WithEvents BtnProtMinusHundred As New Button
    WithEvents BtnProtPlusHundred As New Button
    WithEvents btnProtMoins As New Button
    WithEvents BtnProtPlus As New Button
    WithEvents btnProtMinusTen As New Button
    WithEvents btnProtPlusTen As New Button
    WithEvents BtnProtOui As New Button
    WithEvents BtnProtNon As New Button
    WithEvents BtnProtOui2 As New Button
    WithEvents BtnProtNon2 As New Button
    WithEvents radProtVar1 As New RadioButton
    WithEvents radProtVar2 As New RadioButton
    Dim lblProtNomVar As New Label
    Dim txtProtValeurVar As New TextBox
    Dim lblProtUnite As New Label
    Dim lblProtNomVar2 As New Label
    Dim txtProtValeurVar2 As New TextBox
    Dim lblProtUnite2 As New Label
    Dim lblNomProtocol As New Label
    Dim lblProtChampTexte As New Label
    Dim lblProtChampTexte2 As New Label
    WithEvents txtProtChampTexte As New TextBox
    WithEvents txtProtChampTexte2 As New TextBox
    WithEvents btnProtClavier As New Button
    Dim txtProtlist As New TextBox
    Dim nbChamps As Short = 1
    Dim blValRetour, blValRetour2 As Boolean ' valeur de retour pour les chmaps de type booleen
    Dim strVariavleRetour1, strVariavleRetour2 As String
    Dim methode As String

    'declarations creation objet observation
    
    Public malongitude As Nullable(Of Double)
    Public malatitude As Nullable(Of Double)
    Public monaltitude As String = "Null"
    Public madop As String = "Null"
    Public monEspece As String
    Public monIdEspece As Nullable(Of Integer)
    Public maprecision As Nullable(Of Double)
    Public madescription As String
    Public monIdZoneEtude As Integer ' As Nullable(Of Integer)
    Public maZoneEtude As String
    Public mawptname As String = "Null"
    Public maStation As ClsStation
    Public monObservation As ClsObservation
    Public maIndexStation As Integer
    Private maIdObs As Integer
    Public oObs As ClsObservation
    Public Quality As String = "Null"


    Private Sub frmAffichProtocol_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.GotFocus
        afficherNomProtocole(oProtocole.strProtName)
        ChoixMethodeAjoutControles()
    End Sub

    Private Sub ChoixMethodeAjoutControles()
        Dim strMethode As String = ""
        If oProtocole.strProtField2Name = "Null" Then


            Select Case oProtocole.strProtField1Type
                Case "entier"
                    ajoutControlsChampsTypeEntier(oProtocole.strProtField1Name, oProtocole.strProtField1Unit)
                    strVariavleRetour1 = "entier"
                Case "booleen"
                    ajoutControlsChampsTypeBool(oProtocole.strProtField1Name)
                    strVariavleRetour1 = "booleen"
                Case "texte"
                    ajoutControlsChampsTypeTexte(oProtocole.strProtField1Name)
                    strVariavleRetour1 = "texte"
                Case "liste"
                    ajoutControlsChampsTypeListe(oProtocole.strProtField1Name, oProtocole.strProtListValues)
                    strVariavleRetour1 = "liste"
                Case Else
                    MsgBox("erreur")
            End Select
            strVariavleRetour2 = "Null"
        Else
            ' protocole avec 2 champs
            If oProtocole.strProtField1Type = "entier" Then
                strVariavleRetour1 = "entier"
                Select Case oProtocole.strProtField2Type

                    Case "entier"
                        ajoutControlsChampsTypeEntier(oProtocole.strProtField1Name, oProtocole.strProtField1Unit, oProtocole.strProtField2Name, oProtocole.strProtField2Unit)
                        strVariavleRetour2 = "entier"

                    Case "booleen"
                        ajoutControlsChampsTypeEntier(oProtocole.strProtField1Name, oProtocole.strProtField1Unit, "Null", "Null", 1)
                        ajoutControlsChampsTypeBool("Null", oProtocole.strProtField2Name, 2)
                        strVariavleRetour2 = "booleen"

                    Case "texte"
                        ajoutControlsChampsTypeEntier(oProtocole.strProtField1Name, oProtocole.strProtField1Unit, "Null", "Null", 1)
                        ajoutControlsChampsTypeTexte("Null", oProtocole.strProtField2Name, 2)
                        strVariavleRetour2 = "texte"
                End Select

            ElseIf oProtocole.strProtField1Type = "booleen" Then
                strVariavleRetour1 = "booleen"
                Select Case oProtocole.strProtField2Type
                    Case "booleen"
                        ajoutControlsChampsTypeBool(oProtocole.strProtField1Name, "Null", 1)
                        ajoutControlsChampsTypeBool("Null", oProtocole.strProtField2Name, 2)
                        strVariavleRetour2 = "booleen"
                    Case "texte"
                        ajoutControlsChampsTypeBool(oProtocole.strProtField1Name, "Null", 1)
                        ajoutControlsChampsTypeTexte("Null", oProtocole.strProtField2Name, 1)
                        strVariavleRetour2 = "texte"
                    Case "entier"
                        ajoutControlsChampsTypeBool(oProtocole.strProtField1Name, "Null", 2)
                        ajoutControlsChampsTypeEntier("Null", "Null", oProtocole.strProtField2Name, oProtocole.strProtField2Unit, 1)
                        strVariavleRetour2 = "entier"
                End Select
            ElseIf oProtocole.strProtField1Type = "texte" Then
                strVariavleRetour1 = "texte"
                Select Case oProtocole.strProtField2Type
                    Case "texte"
                        ajoutControlsChampsTypeTexte(oProtocole.strProtField1Name, oProtocole.strProtField2Name, 1)
                        strVariavleRetour2 = "texte"
                    Case "entier"
                        ajoutControlsChampsTypeTexte(oProtocole.strProtField1Name, "Null", 2)
                        ajoutControlsChampsTypeEntier("Null", "Null", oProtocole.strProtField2Name, oProtocole.strProtField2Unit, 1)
                        strVariavleRetour2 = "entier"
                    Case "booleen"
                        ajoutControlsChampsTypeTexte(oProtocole.strProtField1Name, "Null", 1)
                        ajoutControlsChampsTypeBool("Null", oProtocole.strProtField2Name, 2)
                        strVariavleRetour2 = "booleen"
                End Select
            End If

        End If

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'recuperer la valeur du champ 1
        Select Case strVariavleRetour1
            Case "entier"
                oProtocole.strProtField1Value = txtProtValeurVar.Text
                txtProtValeurVar.Text = "0"
            Case "booleen"
                oProtocole.strProtField1Value = blValRetour
            Case "texte"
                oProtocole.strProtField1Value = txtProtChampTexte.Text
                txtProtChampTexte.Text = ""
            Case "liste"
                oProtocole.strProtField1Value = txtProtlist.Text
                txtProtlist.Text = ""
            Case Else
                oProtocole.strProtField1Value = "Null"
        End Select

        'recuperer la valeur du champ 2
        Select Case strVariavleRetour2
            Case "entier"
                oProtocole.strProtField2Value = txtProtValeurVar2.Text
                txtProtValeurVar2.Text = "0"
            Case "booleen"
                oProtocole.strProtField2Value = blValRetour2
            Case "texte"
                oProtocole.strProtField2Value = txtProtChampTexte2.Text
                txtProtChampTexte2.Text = ""
            Case Else
                oProtocole.strProtField2Value = "Null"
        End Select

        ' sauvegarder le protocole utilisé dans un fichier xml (Protocole.xml)et netoyer l'interface du formulaire 
        backup()
        netoyerInterface()
        frmParamProtocole.Show()
        Me.Hide()
    End Sub


#Region "Ajout Controles"

    Public Sub ajoutControlsChampsTypeEntier(ByVal strNomVariable As String, ByVal strUnit As String, Optional ByVal strNomVariable2 As String = "Null", Optional ByVal strUnit2 As String = "Null", Optional ByVal intPositionY As Short = 0)
        Try

            Dim y1, y2, y3, y4, y5, y6, yLoc As Short

            'gérer le cas protocole avec 2 chmaps dont 1 de type entier, placer le champs de type entier en premier
            If intPositionY = 0 Then
                yLoc = 80
            Else
                yLoc = 30
            End If

            ' variable de type entier, protocole à 1 champ
            If (strNomVariable <> "Null" And strNomVariable2 = "Null") Then
                'y3 = yLoc + 10  'emplacement du lblProtNomVar
                y5 = yLoc + 30  'emplacement du txtProtValeurVar
                y1 = yLoc + 5
                y2 = yLoc + 73
                y3 = yLoc + 47
                lblProtNomVar.Location = New Point(11, y3)
                lblProtNomVar.Size = New Size(127, 20)
                lblProtNomVar.Text = strNomVariable
                lblProtNomVar.Name = "lblProtNomVar"
                lblProtNomVar.Font = New Font("Tahoma", 10, FontStyle.Bold)

                txtProtValeurVar.Location = New Point(140, y3)
                txtProtValeurVar.Size = New Size(55, 24)
                txtProtValeurVar.Text = "0"
                txtProtValeurVar.Name = "txtProtValeurVar"
                txtProtValeurVar.Font = New Font("Tahoma", 11, FontStyle.Bold)

                lblProtUnite.Location = New Point(201, y3)
                lblProtUnite.Size = New Size(20, 20)
                lblProtUnite.Text = strUnit
                lblProtUnite.Name = "lblProtUnite"
                lblProtUnite.Font = New Font("Tahoma", 9, FontStyle.Bold)

                Me.Controls.Add(lblProtNomVar)
                Me.Controls.Add(txtProtValeurVar)
                Me.Controls.Add(lblProtUnite)

            ElseIf (strNomVariable <> "Null" And strNomVariable2 <> "Null") Then
                'protocole à 2 champs de type entier
                nbChamps = 2
                yLoc = 25
                y1 = yLoc + 120 'emplacement du bouton +100
                y2 = yLoc + 165 'emplacement du bouton -100

                radProtVar1.Location = New Point(11, 40)
                radProtVar1.Size = New Size(20, 20)
                radProtVar1.Name = "radProtVar1"
                radProtVar1.Checked = True

                radProtVar2.Location = New Point(11, 90)
                radProtVar2.Size = New Size(20, 20)
                radProtVar2.Name = "radProtVar2"
                radProtVar2.Checked = False

                lblProtNomVar.Location = New Point(31, 40)
                lblProtNomVar.Size = New Size(220, 20)
                lblProtNomVar.Text = strNomVariable
                lblProtNomVar.Name = "lblProtNomVar"
                lblProtNomVar.Font = New Font("Tahoma", 10, FontStyle.Bold)

                txtProtValeurVar.Location = New Point(11, 60)
                txtProtValeurVar.Size = New Size(55, 24)
                txtProtValeurVar.Text = "0"
                txtProtValeurVar.Name = "txtProtValeurVar"
                txtProtValeurVar.Font = New Font("Tahoma", 11, FontStyle.Bold)

                lblProtUnite.Location = New Point(70, 60)
                lblProtUnite.Size = New Size(20, 20)
                lblProtUnite.Text = strUnit
                lblProtUnite.Font = New Font("Tahoma", 9, FontStyle.Bold)

                lblProtNomVar2.Location = New Point(31, 90)
                lblProtNomVar2.Size = New Size(127, 20)
                lblProtNomVar2.Text = strNomVariable2
                lblProtNomVar2.Name = "lblProtNomVar2"
                lblProtNomVar2.Font = New Font("Tahoma", 10, FontStyle.Bold)

                txtProtValeurVar2.Location = New Point(11, 110)
                txtProtValeurVar2.Size = New Size(55, 24)
                txtProtValeurVar2.Text = "0"
                txtProtValeurVar2.Name = "txtProtValeurVar2"
                txtProtValeurVar2.Font = New Font("Tahoma", 11, FontStyle.Bold)

                lblProtUnite2.Location = New Point(70, 110)
                lblProtUnite2.Size = New Size(20, 20)
                lblProtUnite2.Text = strUnit2
                lblProtUnite2.Name = "lblProtUnite2"
                lblProtUnite2.Font = New Font("Tahoma", 9, FontStyle.Bold)

                Me.Controls.Add(radProtVar1)
                Me.Controls.Add(radProtVar2)
                Me.Controls.Add(lblProtNomVar)
                Me.Controls.Add(txtProtValeurVar)
                Me.Controls.Add(lblProtUnite)
                Me.Controls.Add(lblProtNomVar2)
                Me.Controls.Add(txtProtValeurVar2)
                Me.Controls.Add(lblProtUnite2)
            ElseIf (strNomVariable = "Null" And strNomVariable2 <> "Null") Then
                y4 = yLoc + 47  'emplacement du lblProtNomVar2
                y6 = yLoc + 47  'emplacement du txtProtValeurVar2
                y1 = yLoc  'emplacement du bouton +100
                y2 = yLoc + 73 'emplacement du bouton -100


                lblProtNomVar2.Location = New Point(11, y4)
                lblProtNomVar2.Size = New Size(127, 20)
                lblProtNomVar2.Text = strNomVariable2
                lblProtNomVar2.Name = "lblProtNomVar2"
                lblProtNomVar2.Font = New Font("Tahoma", 10, FontStyle.Bold)

                txtProtValeurVar2.Location = New Point(140, y6)
                txtProtValeurVar2.Size = New Size(55, 24)
                txtProtValeurVar2.Text = "0"
                txtProtValeurVar2.Name = "txtProtValeurVar2"
                txtProtValeurVar2.Font = New Font("Tahoma", 11, FontStyle.Bold)

                lblProtUnite2.Location = New Point(201, y6)
                lblProtUnite2.Size = New Size(20, 20)
                lblProtUnite2.Text = strUnit2
                lblProtUnite2.Name = "lblProtUnite2"
                lblProtUnite2.Font = New Font("Tahoma", 9, FontStyle.Bold)
                Me.Controls.Add(lblProtNomVar2)
                Me.Controls.Add(txtProtValeurVar2)
                Me.Controls.Add(lblProtUnite2)
            End If

            BtnProtPlusHundred.Location = New Point(11, y1)
            BtnProtPlusHundred.Size = New Size(82, 37)
            BtnProtPlusHundred.Name = "BtnProtPlusHundred"
            BtnProtPlusHundred.Image = My.Resources.Images.btnPlus100

            BtnProtMinusHundred.Location = New Point(11, y2)
            BtnProtMinusHundred.Size = New Size(82, 37)
            BtnProtMinusHundred.Name = "BtnProtMinusHundred"
            BtnProtMinusHundred.Image = My.Resources.Images.btnPlus100


            btnProtPlusTen.Location = New Point(102, y1)
            btnProtPlusTen.Size = New Size(66, 37)
            btnProtPlusTen.Name = "btnProtPlusTen"
            btnProtPlusTen.Image = My.Resources.Images.btnPlus10
 
            btnProtMinusTen.Location = New Point(102, y2)
            btnProtMinusTen.Size = New Size(66, 37)
            btnProtMinusTen.Name = "btnProtMinusTen"
            btnProtMinusTen.Image = My.Resources.Images.btnMoins10
      
            BtnProtPlus.Location = New Point(178, y1)
            BtnProtPlus.Size = New Size(50, 37)
            BtnProtPlus.Name = "BtnProtPlus"
            BtnProtPlus.Image = My.Resources.Images.btnPlus1

            btnProtMoins.Location = New Point(178, y2)
            btnProtMoins.Size = New Size(50, 37)
            btnProtMoins.Image = My.Resources.Images.btnMoins1
            btnProtMoins.Name = "btnProtMoins"

            Me.Controls.Add(BtnProtMinusHundred)
            Me.Controls.Add(BtnProtPlusHundred)
            Me.Controls.Add(btnProtMoins)
            Me.Controls.Add(BtnProtPlus)
            Me.Controls.Add(btnProtMinusTen)
            Me.Controls.Add(btnProtPlusTen)

        Catch ex As Exception
            MsgBox("erreur dans le type de variable")
        End Try
    End Sub
    Private Sub ajoutControlsChampsTypeBool(ByVal strNomVariable As String, Optional ByVal strNomVariable2 As String = "Null", Optional ByVal intPosition As Short = 0)
        ' "intPosition" pour gerer l'affichage de de 2 champs mixtes (type bool + autre )
        ' valeur par defaut =0, 1 pour controles bool affichés en premier, 2 pour controles bool affichés en 2 ème position

        Dim y As Short

        oProtocole.strProtField1Unit = ""
        Select Case intPosition
            Case 0
                y = 80
            Case 1
                y = 40
            Case 2
                y = 160
        End Select
        If strNomVariable <> "Null" Then
            lblProtNomVar.Location = New Point(11, y) ' y=80
            lblProtNomVar.Size = New Size(127, 20)
            lblProtNomVar.Text = strNomVariable
            lblProtNomVar.Name = "lblProtNomVar"
            lblProtNomVar.Font = New Font("Tahoma", 10, FontStyle.Bold)

            BtnProtOui.Location = New Point(11, y + 20)
            BtnProtOui.Size = New Size(55, 37)
            BtnProtOui.Name = "BtnProtOui"
            BtnProtOui.Image = My.Resources.Images.btnOui

            BtnProtNon.Location = New Point(88, y + 20)
            BtnProtNon.Size = New Size(55, 37)
            BtnProtNon.Name = "BtnProtNon"
            BtnProtNon.Image = My.Resources.Images.btnNon

            Me.Controls.Add(BtnProtOui)
            Me.Controls.Add(BtnProtNon)
            Me.Controls.Add(lblProtNomVar)
        End If
        If strNomVariable2 <> "Null" Then

            lblProtNomVar2.Location = New Point(11, y) ' y=80
            lblProtNomVar2.Size = New Size(127, 20)
            lblProtNomVar2.Text = strNomVariable2
            lblProtNomVar2.Name = "lblProtNomVar2"
            lblProtNomVar2.Font = New Font("Tahoma", 10, FontStyle.Bold)

            BtnProtOui2.Location = New Point(11, y + 20)
            BtnProtOui2.Size = New Size(55, 37)
            BtnProtOui2.Name = "BtnProtOui2"
            BtnProtOui2.Image = My.Resources.Images.btnOui

            BtnProtNon2.Location = New Point(88, y + 20)
            BtnProtNon2.Size = New Size(55, 37)
            BtnProtNon2.Name = "BtnProtNon2"
            BtnProtNon2.Image = My.Resources.Images.btnNon

            Me.Controls.Add(BtnProtOui2)
            Me.Controls.Add(BtnProtNon2)
            Me.Controls.Add(lblProtNomVar2)

        End If

    End Sub
    Private Sub ajoutControlsChampsTypeTexte(ByVal strNomVariable As String, Optional ByVal strNomVariable2 As String = "Null", Optional ByVal intPosition As Short = 0)
        Dim y As Short

        Select Case intPosition
            Case 0
                y = 40
            Case 1
                y = 40
            Case 2
                y = 80
        End Select
        If strNomVariable <> "Null" Then
            If strNomVariable2 = "Null" And intPosition = 2 Then
                y = 150
            End If
            lblProtChampTexte.Location = New Point(11, y)
            lblProtChampTexte.Size = New Size(200, 20)
            lblProtChampTexte.Text = strNomVariable
            lblProtChampTexte.Name = "lblProtChampTexte"
            lblProtChampTexte.Font = New Font("Tahoma", 10, FontStyle.Bold)

            txtProtChampTexte.Location = New Point(11, y + 30)
            txtProtChampTexte.Size = New Size(200, 20)
            txtProtChampTexte.Text = "Saisir le texte"
            txtProtChampTexte.Name = "txtProtChampTexte"
            txtProtChampTexte.Font = New Font("Tahoma", 10, FontStyle.Regular)

            Me.Controls.Add(lblProtChampTexte)
            Me.Controls.Add(txtProtChampTexte)
        End If
        If strNomVariable2 <> "Null" Then

            lblProtChampTexte2.Location = New Point(11, y + 70)
            lblProtChampTexte2.Size = New Size(200, 20)
            lblProtChampTexte2.Text = strNomVariable2
            lblProtChampTexte2.Name = "lblProtChampTexte"
            lblProtChampTexte2.Font = New Font("Tahoma", 10, FontStyle.Bold)

            txtProtChampTexte2.Location = New Point(11, y + 100)
            txtProtChampTexte2.Size = New Size(200, 20)
            txtProtChampTexte2.Text = "Saisir le texte"
            txtProtChampTexte2.Name = "txtProtChampTexte2"
            txtProtChampTexte2.Font = New Font("Tahoma", 10, FontStyle.Regular)

            Me.Controls.Add(lblProtChampTexte2)
            Me.Controls.Add(txtProtChampTexte2)

        End If

        btnProtClavier.Location = New Point(11, 230)
        btnProtClavier.Size = New Size(170, 25)
        btnProtClavier.Text = "Clavier"
        btnProtClavier.Name = "btnProtClavier"
        btnProtClavier.Font = New Font("Tahoma", 10, FontStyle.Bold)

        Me.Controls.Add(btnProtClavier)
    End Sub
    Sub ajoutControlsChampsTypeListe(ByVal strNomVariable As String, ByVal listeValeurs As String)
        Dim i, j, k, l, s As Short
        s = 1

        i = Int(listeValeurs.Split(";")(0))
        k = Int(listeValeurs.Split(";")(1))

        If i < 5 And k < 115 Or k > 12 Then


            For j = 2 To i + 1
                Dim x As New Button()

                l = Math.Max(50, 10 * k)
                x.Name = "btnProt" & j.ToString
                x.Size = New Size(l, 30)
                x.BackColor = Color.LightGray
                x.Location = New Point((240 - l) / 2, (j - 1) * 40)
                x.Text = listeValeurs.Split(";")(j)
                Controls.Add(x)
                AddHandler x.Click, AddressOf EntreeListe
            Next
            txtProtlist.Location = New Point((240 - l) / 2, (j - 1) * 40)
        ElseIf i < 10 And k < 115 Then

            For j = 2 To Int(i / 2) + 2
                Dim x As New Button()

                l = Math.Max(50, 10 * k)
                x.Name = "btnProt" & j.ToString
                x.Size = New Size(l, 30)
                x.BackColor = Color.LightGray
                x.Location = New Point((240 - 2 * l) / 3, (j - 1) * 40)
                x.Text = listeValeurs.Split(";")(j)
                Controls.Add(x)
                AddHandler x.Click, AddressOf EntreeListe
            Next

            For j = Int(i / 2) + 3 To i + 1
                Dim x As New Button()

                x.Name = "btnProt" & j.ToString
                x.Size = New Size(l, 30)
                x.BackColor = Color.LightGray
                x.Location = New Point(l + (2 * (240 - 2 * l) / 3), (s * 40))
                x.Text = listeValeurs.Split(";")(j)
                s += 1
                Controls.Add(x)
                AddHandler x.Click, AddressOf EntreeListe
            Next
            txtProtlist.Location = New Point(l + (2 * (240 - 2 * l) / 3), s * 40)

        End If

        txtProtlist.Size = New Size(l, 30)
        txtProtlist.Name = "txtProtlist"

        Controls.Add(txtProtlist)
    End Sub

#End Region
#Region "Evenements"

    Private Sub EntreeListe(ByVal sender As Object, ByVal e As EventArgs)
        Dim val As String = ""
        Dim i As Short
        For i = 0 To Controls.Count - 1
            If (sender.Equals(Controls(i))) Then
                val = Controls(i).Text
            End If
        Next

        txtProtlist.Text = val

    End Sub

    Private Sub BtnProtPlusHundred_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnProtPlusHundred.Click
        Dim i As Integer
        Dim compteur As Integer = 0

        If strVariavleRetour1 = "entier" And strVariavleRetour2 <> "entier" Then


            i = CType(txtProtValeurVar.Text, Integer)
            compteur = i + 100
            txtProtValeurVar.Text = compteur


        ElseIf strVariavleRetour2 = "entier" And strVariavleRetour1 <> "entier" Then
            i = CType(txtProtValeurVar2.Text, Integer)
            compteur = i + 100
            txtProtValeurVar2.Text = compteur

        ElseIf strVariavleRetour2 = "entier" And strVariavleRetour1 = "entier" Then
            If radProtVar1.Checked = True Then
                i = CType(txtProtValeurVar.Text, Integer)
                compteur = i + 100
                txtProtValeurVar.Text = compteur
            ElseIf radProtVar2.Checked = True Then
                i = CType(txtProtValeurVar2.Text, Integer)
                compteur = i + 100
                txtProtValeurVar2.Text = compteur
            End If
        End If

    End Sub
    Private Sub BtnProtMinusHundred_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnProtMinusHundred.Click
        Dim i As Integer
        Dim compteur As Integer = 0

        If strVariavleRetour1 = "entier" And strVariavleRetour2 <> "entier" Then

            i = CType(txtProtValeurVar.Text, Integer)

            If CType(txtProtValeurVar.Text, Integer) > 0 Then
                compteur = i - 100
                txtProtValeurVar.Text = IIf(compteur < 0, 0, compteur)
            Else
                txtProtValeurVar.Text = 0
            End If

        ElseIf strVariavleRetour2 = "entier" And strVariavleRetour1 <> "entier" Then


            i = CType(txtProtValeurVar2.Text, Integer)

            If CType(txtProtValeurVar2.Text, Integer) > 0 Then
                compteur = i - 100
                txtProtValeurVar2.Text = IIf(compteur < 0, 0, compteur)
            Else
                txtProtValeurVar2.Text = 0
            End If

        ElseIf strVariavleRetour2 = "entier" And strVariavleRetour1 = "entier" Then
            If radProtVar1.Checked = True Then
                i = CType(txtProtValeurVar.Text, Integer)
                If CType(txtProtValeurVar.Text, Integer) > 0 Then
                    compteur = i - 100
                    txtProtValeurVar.Text = IIf(compteur < 0, 0, compteur)
                Else
                    txtProtValeurVar.Text = 0
                End If
            ElseIf radProtVar2.Checked = True Then
                i = CType(txtProtValeurVar2.Text, Integer)
                If CType(txtProtValeurVar2.Text, Integer) > 0 Then
                    compteur = i - 100
                    txtProtValeurVar2.Text = IIf(compteur < 0, 0, compteur)
                Else
                    txtProtValeurVar2.Text = 0
                End If

            End If


        End If

    End Sub

    Private Sub btnProtPlusTen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnProtPlusTen.Click
        Dim i As Integer
        Dim compteur As Integer = 0

        If strVariavleRetour1 = "entier" And strVariavleRetour2 <> "entier" Then
            i = CType(txtProtValeurVar.Text, Integer)
            compteur = i + 10
            txtProtValeurVar.Text = compteur

        ElseIf strVariavleRetour2 = "entier" And strVariavleRetour1 <> "entier" Then
            i = CType(txtProtValeurVar2.Text, Integer)
            compteur = i + 10
            txtProtValeurVar2.Text = compteur

        ElseIf strVariavleRetour2 = "entier" And strVariavleRetour1 = "entier" Then

            If radProtVar1.Checked = True Then
                i = CType(txtProtValeurVar.Text, Integer)
                compteur = i + 10
                txtProtValeurVar.Text = compteur
            ElseIf radProtVar2.Checked = True Then
                i = CType(txtProtValeurVar2.Text, Integer)
                compteur = i + 10
                txtProtValeurVar2.Text = compteur
            End If
        End If

    End Sub

    Private Sub btnProtMinusTen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnProtMinusTen.Click
        Dim i As Integer
        Dim compteur As Integer = 0


        If strVariavleRetour1 = "entier" And strVariavleRetour2 <> "entier" Then
            i = CType(txtProtValeurVar.Text, Integer)
            If CType(txtProtValeurVar.Text, Integer) > 0 Then
                compteur = i - 10
                txtProtValeurVar.Text = IIf(compteur < 0, 0, compteur)
            Else
                txtProtValeurVar.Text = 0
            End If

        ElseIf strVariavleRetour2 = "entier" And strVariavleRetour1 <> "entier" Then
            i = CType(txtProtValeurVar2.Text, Integer)
            If CType(txtProtValeurVar2.Text, Integer) > 0 Then
                compteur = i - 10
                txtProtValeurVar2.Text = IIf(compteur < 0, 0, compteur)
            Else
                txtProtValeurVar2.Text = 0
            End If

        ElseIf strVariavleRetour2 = "entier" And strVariavleRetour1 = "entier" Then

            If radProtVar1.Checked = True Then
                i = CType(txtProtValeurVar.Text, Integer)
                If CType(txtProtValeurVar.Text, Integer) > 0 Then
                    compteur = i - 10
                    txtProtValeurVar.Text = IIf(compteur < 0, 0, compteur)
                Else
                    txtProtValeurVar.Text = 0
                End If
            ElseIf radProtVar2.Checked = True Then
                i = CType(txtProtValeurVar2.Text, Integer)
                If CType(txtProtValeurVar2.Text, Integer) > 0 Then
                    compteur = i - 10
                    txtProtValeurVar2.Text = IIf(compteur < 0, 0, compteur)
                Else
                    txtProtValeurVar2.Text = 0
                End If
            End If
        End If

    End Sub

    Private Sub btnProtMoins_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnProtMoins.Click
        Dim i As Integer
        Dim compteur As Integer = 0

        If strVariavleRetour1 = "entier" And strVariavleRetour2 <> "entier" Then
            i = CType(txtProtValeurVar.Text, Integer)
            If CType(txtProtValeurVar.Text, Integer) > 0 Then
                compteur = i - 1
                txtProtValeurVar.Text = IIf(compteur < 0, 0, compteur)
            Else
                txtProtValeurVar.Text = 0
            End If

        ElseIf strVariavleRetour2 = "entier" And strVariavleRetour1 <> "entier" Then
            i = CType(txtProtValeurVar2.Text, Integer)
            If CType(txtProtValeurVar2.Text, Integer) > 0 Then
                compteur = i - 1
                txtProtValeurVar2.Text = IIf(compteur < 0, 0, compteur)
            Else
                txtProtValeurVar2.Text = 0
            End If

        ElseIf strVariavleRetour2 = "entier" And strVariavleRetour1 = "entier" Then

            If radProtVar1.Checked = True Then
                i = CType(txtProtValeurVar.Text, Integer)
                If CType(txtProtValeurVar.Text, Integer) > 0 Then
                    compteur = i - 1
                    txtProtValeurVar.Text = IIf(compteur < 0, 0, compteur)
                Else
                    txtProtValeurVar.Text = 0
                End If
            ElseIf radProtVar2.Checked = True Then
                i = CType(txtProtValeurVar2.Text, Integer)
                If CType(txtProtValeurVar2.Text, Integer) > 0 Then
                    compteur = i - 1
                    txtProtValeurVar2.Text = IIf(compteur < 0, 0, compteur)
                Else
                    txtProtValeurVar2.Text = 0
                End If
            End If
        End If


    End Sub


    Private Sub BtnProtPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnProtPlus.Click

        Dim i As Integer
        Dim compteur As Integer = 0

        If strVariavleRetour1 = "entier" And strVariavleRetour2 <> "entier" Then
            i = CType(txtProtValeurVar.Text, Integer)
            compteur = i + 1
            txtProtValeurVar.Text = compteur

        ElseIf strVariavleRetour2 = "entier" And strVariavleRetour1 <> "entier" Then
            i = CType(txtProtValeurVar2.Text, Integer)
            compteur = i + 1
            txtProtValeurVar2.Text = compteur

        ElseIf strVariavleRetour2 = "entier" And strVariavleRetour1 = "entier" Then

            If radProtVar1.Checked = True Then
                i = CType(txtProtValeurVar.Text, Integer)
                compteur = i + 1
                txtProtValeurVar.Text = compteur
            ElseIf radProtVar2.Checked = True Then
                i = CType(txtProtValeurVar2.Text, Integer)
                compteur = i + 1
                txtProtValeurVar2.Text = compteur
            End If
        End If
    End Sub
    Private Sub afficherNomProtocole(ByVal strNomProtocole As String)
        lblNomProtocol.Location = New Point(5, 2)
        lblNomProtocol.Size = New Size(220, 20)
        lblNomProtocol.Text = strNomProtocole
        lblNomProtocol.Font = New Font("Tahoma", 12, FontStyle.Bold)
        Me.Controls.Add(lblNomProtocol)
    End Sub


    Private Sub radProtVar1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radProtVar1.CheckedChanged, BtnProtPlusHundred.Click, BtnProtMinusHundred.Click, btnProtPlusTen.Click, btnProtMinusTen.Click, btnProtMoins.Click, BtnProtPlus.Click

    End Sub
    Private Sub radProtVar2_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radProtVar2.CheckedChanged, BtnProtPlusHundred.Click, BtnProtMinusHundred.Click, btnProtPlusTen.Click, btnProtMinusTen.Click, btnProtMoins.Click, BtnProtPlus.Click

    End Sub
    Private Sub BtnProtNon_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnProtNon.Click
        BtnProtNon.Image = My.Resources.Images.btnNonSelected
        BtnProtOui.Image = My.Resources.Images.btnOui
        BtnProtOui.Refresh()
        blValRetour = False
    End Sub
    Private Sub BtnProtNon2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnProtNon2.Click

        BtnProtNon2.Image = My.Resources.Images.btnNonSelected
        BtnProtOui2.Image = My.Resources.Images.btnOui
        BtnProtOui2.Refresh()
        blValRetour2 = False
    End Sub

    Private Sub BtnProtOui_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnProtOui.Click
        BtnProtOui.Image = My.Resources.Images.btnOuiSelected
        BtnProtNon.Image = My.Resources.Images.btnNon
        BtnProtNon.Refresh()
        blValRetour = True
    End Sub

    Private Sub BtnProtOui2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnProtOui2.Click
        BtnProtOui2.Image = My.Resources.Images.btnOuiSelected
        BtnProtNon2.Image = My.Resources.Images.btnNon
        BtnProtNon2.Refresh()
        blValRetour2 = True
    End Sub

    Private Sub txtProtChampTexte_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtProtChampTexte.GotFocus
        txtProtChampTexte.Text = ""
    End Sub
    Private Sub txtProtChampTexte2_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtProtChampTexte2.GotFocus
        txtProtChampTexte2.Text = ""
    End Sub


    Private Sub netoyerInterface()
        Dim controle As New Control
        Dim strChaine As String

        For Each controle In Me.Controls

            strChaine = controle.Name
            If strChaine <> "" Then
                strChaine = strChaine.Substring(3, 4)
                If (strChaine = "Prot") Then
                    Me.Controls.Remove(controle)
                End If
            End If
        Next
    End Sub

    Private Sub btnProtClavier_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnProtClavier.Click
        Me.txtProtChampTexte.Focus()
        SIP.Enabled = True
    End Sub
#End Region


    Public Sub SaveProtocole(ByVal strDirectoryName As String, ByRef oProtocole As ClsProtocole)
        Dim strDataSource As String
        'sauvegarde du protocole
        If (oProtocole.strProtName <> "Null") Then
            If (File.Exists(strDirectoryName & "Protocole.XML")) Then
                'Suppression de l'ancien fichier
                File.Delete(strDirectoryName & "Protocole.XML")
            End If
            Try
                'Sauvegarde des données
                strDataSource = oProtocole.ExportProtocoleToXML
                SaveString(strDirectoryName & "Protocole.XML", strDataSource)
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Pocket eRelevé")
            End Try
        End If
    End Sub

    Sub backup()
        Try
            SaveProtocole(FormMain.NSDossier, oProtocole)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Pocket eRelevé")
        End Try
    End Sub

    Private Sub BtnSuivant_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSuivant.Click

        Try
            'recuperer la valeur du champ 1
            Select Case strVariavleRetour1
                Case "entier"
                    oProtocole.strProtField1Value = txtProtValeurVar.Text
                    txtProtValeurVar.Text = "0"
                Case "booleen"
                    oProtocole.strProtField1Value = blValRetour
                Case "texte"
                    oProtocole.strProtField1Value = txtProtChampTexte.Text
                    txtProtChampTexte.Text = ""
                Case "liste"
                    oProtocole.strProtField1Value = txtProtlist.Text
                    txtProtlist.Text = ""
                Case Else
                    oProtocole.strProtField1Value = "Null"
            End Select

            'recuperer la valeur du champ 2
            Select Case strVariavleRetour2
                Case "entier"
                    oProtocole.strProtField2Value = txtProtValeurVar2.Text
                    txtProtValeurVar2.Text = "0"
                Case "booleen"
                    oProtocole.strProtField2Value = blValRetour2
                Case "texte"
                    oProtocole.strProtField2Value = txtProtChampTexte2.Text
                    txtProtChampTexte2.Text = ""
                Case Else
                    oProtocole.strProtField2Value = "Null"
            End Select

            

            frmEntryFin.dEntryStep = 5
            'Sauvegarde des valeurs saisies pendant la séquence
            'Abondance
            ' On choisit comme abondance la valeur en face du bouton sélectionné (nombre d'individus ou indice d'abondance)


            'oObs = New ClsObservation(monIdEspece, monEspece, Nothing, val_Ind2, val_Distance, val_Occup)
            oObs = New ClsObservation(monIdEspece, monEspece, Quality, frmEntryChoixTaxon.strNomPhoto, oProtocole.strProtName, oProtocole.strProtField1Name, _
                                      oProtocole.strProtField1Type, oProtocole.strProtField1Unit, oProtocole.strProtField1Value, oProtocole.strProtField2Name, _
                                      oProtocole.strProtField2Type, oProtocole.strProtField2Unit, oProtocole.strProtField2Value)

            If maIndexStation <> 0 Then
                FormMain.oListObs.lstStations().Item(maIndexStation).AddOneObjectObservation(oObs)
            Else
                maStation = New ClsStation(System.DateTime.Now, _
                                           mawptname, malongitude, malatitude, monaltitude, madop, maprecision, monIdZoneEtude, maZoneEtude, madescription, _
                                           frmParamObservateur.intIdObs1, frmParamObservateur.CmbObs1.Text, frmParamObservateur.intIdObs2, frmParamObservateur.CmbObs2.Text, 1)
                maStation.AddOneObjectObservation(oObs)
                maIndexStation = FormMain.oListObs.AddOneObjectStation(maStation)
            End If


            ' sauvegarder le nom du form en cours pour y retourner si on choisit de saisir une nouvelle valeur
            '  frmEntryFin.formPrecedent = "frmEntryWhat_Density"
            backup()
            netoyerInterface()
            frmEntryFin.Show()
            Me.Hide()
        Catch ex As Exception
            MsgBox(DirectCast(My.Resources.Images.errorOnSave, String) & ex.Message, MsgBoxStyle.Critical, "Pocket eRelevé")
        End Try




    End Sub

    Private Sub BtnRetour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRetour.Click

        'frmEntryFin.dEntryStep = 3
        frmEntryChoixTaxon.Show()
        Me.Hide()
    End Sub

    
    Private Sub BtnMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnMenu.Click
        FormMain.Show()
        Me.Hide()
    End Sub
End Class